/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.services.saving;

/**
 *
 * @author jay
 */

import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import src.HibernateUtil;
import src.Menu;
import src.domain.saving.Menusavtrans;
import src.domain.saving.MenusavtransId;
import src.services.CRUEDService;

public class MenusavtransModel implements java.io.Serializable {
    
    private CRUEDService service;
    
    public MenusavtransModel() {
        service = new CRUEDService();
    }

    public List<Menusavtrans> findAll() {
        Session session;
        
        List result = new ArrayList();
        List<Menu> temp;
        
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        org.hibernate.Transaction tx = session.beginTransaction();
              
        try {
             
            String hql = "from Menu menu where menu.menuGroup in('SAVING', 'TERM_DEPOSIT', 'LOAN') and menu.menuType = 'TRANSACTION' "+
                    "order by menu.menuId";
                    
            temp = session.createQuery(hql).list();
            
            for (int i=0; i < temp.size(); i++) {
                
                Menusavtrans menutrx = new Menusavtrans();
                Menu menu = temp.get(i);
                                                
                Query qt = session.createQuery(
                        "select mt.savtrans.savtransId from Menusavtrans mt where mt.menu.menuId = :menuId")
                        .setInteger("menuId", menu.getMenuId());
                
                List<Integer> lsTrx = (List) qt.list();
                
                menutrx.setMenu(menu);
                menutrx.setSavtransIds(lsTrx);
                result.add(menutrx);
            }
              
            tx.commit();
            
        } catch (RuntimeException e) {
            
            if (tx != null && tx.isActive()) {
                try {tx.rollback(); } 
                catch (HibernateException e1) {}
                throw e;
            }
        }
        
        finally { }
        
        return result;
    }
    

    public boolean saveData(List<Menusavtrans> data) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        org.hibernate.Transaction tx = session.beginTransaction();
        boolean success;
        
        try {
            
            List<Menusavtrans> trxs = new  ArrayList<>();
            
            Query q = session.createQuery("delete from Menusavtrans men");
            q.executeUpdate();
            
            for (int i=0; i< data.size(); i++) {
                
                List<Integer> listId = data.get(i).getSavtransIds();
                
                if (listId != null) {
                    
                    for (int j=0; j< listId.size(); j++) {
                        Menusavtrans trx = new Menusavtrans();
                       // Savtrans savtrans = new Savtrans();
                        
                        Integer menuId = data.get(i).getMenu().getMenuId();
                        
                        Object savIdObj = (Object) listId.get(j);
                        String savIdStr = (String) savIdObj;
                        Integer savId = Integer.parseInt(savIdStr);
                        
                        trx.setId(new MenusavtransId(menuId, savId));
                        session.saveOrUpdate(trx);
                        trxs.add(trx);
                    }
                }
            }
            
                     
            tx.commit();
            success = true;  
            
        } catch (RuntimeException e) {
            
            if (tx != null && tx.isActive()) {
                try {
                    tx.rollback();
                } catch (HibernateException e1) {}

                throw e;
            }
            success = false;  
        }

        return success;
    }
    
    
}
